package easy;

import util.PrintUtils;

public class Solution_977 {

    public int[] sortedSquares(int[] nums) {
        int[] array = new int[10001];
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] < 0) {
                array[-nums[i]]++;
            } else {
                array[nums[i]]++;
            }
        }
        for (int i = 0, index = 0; i < array.length && index < nums.length; i++) {
            if (array[i] > 0) {
                int s = i * i;
                for (int j = 0; j < array[i]; j++) {
                    nums[index++] = s;
                }
            }
        }
        return nums;
    }

    /*public int[] sortedSquares(int[] nums) {
        if (nums[nums.length-1]<=0) {

        } else if (nums[0] < 0) {
            int lastIndex = nums.length - 1;
            int num = Math.negateExact(nums[0]);
            for (int i = 1; i < nums.length; i++) {
                if (nums[i] > num) {
                    lastIndex = i - 1;
                    break;
                }
            }
            for (int i = 0; i < lastIndex; ) {
                int num1 = Math.abs(nums[i]);
                int num2 = Math.abs(nums[lastIndex]);
                if (num1 <= num2) {
                    break;
                }
                nums[lastIndex--] = num1;
                nums[i] = num2;
            }
        }
        for (int i = 0; i < nums.length; i++) {
            nums[i] = nums[i] * nums[i];
        }
        return nums;
    }*/

    public static void main(String[] args) {
        Solution_977 model = new Solution_977();
        PrintUtils.arrayInt(model.sortedSquares(new int[]{-5, -3, -2, -1}));
        PrintUtils.arrayInt(model.sortedSquares(new int[]{-4, -1, 0, 3, 10}));
        PrintUtils.arrayInt(model.sortedSquares(new int[]{-7, -3, 2, 3, 11}));
    }
}
